package www.xfOnline.ui;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import java.awt.Insets;
import javax.swing.JButton;

import www.xfOnline.bean.User;
import www.xfOnline.com.Login;
import www.xfOnline.dao.JDBCDAO;
import www.xfOnline.util.Email;
import www.xfOnline.util.VAR;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.util.Date;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JPasswordField;

public class RegFrame extends JFrame {
	private JTextField tfName;
	private JTextField tfEmail;
	private JTextField tfYan;
	private JDBCDAO jdao;
	private User user;
	private JLabel labelName;
	private JLabel labelYan;
	private String name;
	private String email;
	private String password;
	private JLabel label;
	private JPasswordField tfPassword;
	private JLabel labelPassword;
	private JLabel label_1;
	private JPasswordField tfPasswordCnfir;
	/**
	 * Create the frame.
	 * 注册界面。
	 * 后期加了点非空判断（解决点问题），就不加界面提示了。
	 */
	public RegFrame() {
		try {
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (InstantiationException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IllegalAccessException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (UnsupportedLookAndFeelException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		jdao = new JDBCDAO();
		user = new User();
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		Dimension scressSize = getToolkit().getScreenSize();
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		setBounds((int)scressSize.getWidth()/3, (int)scressSize.getHeight()/3, (int)scressSize.getWidth()/3, (int)scressSize.getHeight()/3);
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0,0,0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0,0,0,0};
		gridBagLayout.columnWeights = new double[]{1.0, 1.0,1.0, 1.0, 1.0, 1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{1.0,1.0,1.0, 1.0,1.0,1.0, 2.0,2.0,3.0, Double.MIN_VALUE};
		getContentPane().setLayout(gridBagLayout);
		
		JLabel lblNewLabel = new JLabel("姓名");
		GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
		gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel.gridx = 1;
		gbc_lblNewLabel.gridy = 2;
		getContentPane().add(lblNewLabel, gbc_lblNewLabel);
		
		tfName = new JTextField();
		tfName.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
				labelEmpty();
			}
		});
		GridBagConstraints gbc_tfName = new GridBagConstraints();
		gbc_tfName.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfName.insets = new Insets(0, 0, 5, 5);
		gbc_tfName.gridx = 2;
		gbc_tfName.gridy = 2;
		getContentPane().add(tfName, gbc_tfName);
		tfName.setColumns(10);
		
		labelName = new JLabel("");
		GridBagConstraints gbc_labelName = new GridBagConstraints();
		gbc_labelName.insets = new Insets(0, 0, 5, 5);
		gbc_labelName.gridx = 3;
		gbc_labelName.gridy = 2;
		getContentPane().add(labelName, gbc_labelName);
		
		JLabel lblNewLabel_1 = new JLabel("邮箱");
		GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
		gbc_lblNewLabel_1.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_1.gridx = 1;
		gbc_lblNewLabel_1.gridy = 3;
		getContentPane().add(lblNewLabel_1, gbc_lblNewLabel_1);
		
		tfEmail = new JTextField();
		tfEmail.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent arg0) {
				labelEmpty();
				String sql = "select uid from user where name = ?";
				User u=null;
				name=tfName.getText();
				if(name.isEmpty()){
					return;
				}
				u=jdao.findOne(sql, new Object[]{name}, user.getClass());
				if(u!=null){
					labelName.setText("用户名已存在");
				}
			}
		});
		GridBagConstraints gbc_tfEmail = new GridBagConstraints();
		gbc_tfEmail.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfEmail.insets = new Insets(0, 0, 5, 5);
		gbc_tfEmail.gridx = 2;
		gbc_tfEmail.gridy = 3;
		getContentPane().add(tfEmail, gbc_tfEmail);
		tfEmail.setColumns(10);
		
		JButton btnNewButton = new JButton("发送验证码");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				labelEmpty();
				email = tfEmail.getText();
				String sql = "select uid from user where email = ?";
				User u=null;
				String merchEmailStr="^([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)*@([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)+[\\.][A-Za-z]{2,3}([\\.][A-Za-z]{2})?$";
				if(email.isEmpty()){
					return;
				}
				if(!email.matches(merchEmailStr)){
					labelName.setText("邮箱格式不正确");
					return;
				}
				u=jdao.findOne(sql, new Object[]{email}, user.getClass());
				if(u!=null){
					labelName.setText("邮箱已存在");
					return;
				}
				VAR.sixNum = VAR.getSixRandom();
				Email mymail = new Email();
				mymail.toAddress(email, "验证码", Integer.toString(VAR.sixNum));
				Timer timer = new Timer(); 
			    timer.schedule(new MyTask(),300000,300000);
			    labelYan.setText("验证码已发送，请在5分钟内输入。");
			}
		});
		GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
		gbc_btnNewButton.anchor = GridBagConstraints.WEST;
		gbc_btnNewButton.insets = new Insets(0, 0, 5, 5);
		gbc_btnNewButton.gridx = 3;
		gbc_btnNewButton.gridy = 3;
		getContentPane().add(btnNewButton, gbc_btnNewButton);
		
		JLabel lblNewLabel_2 = new JLabel("验证码");
		GridBagConstraints gbc_lblNewLabel_2 = new GridBagConstraints();
		gbc_lblNewLabel_2.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_2.gridx = 1;
		gbc_lblNewLabel_2.gridy = 4;
		getContentPane().add(lblNewLabel_2, gbc_lblNewLabel_2);
		
		tfYan = new JTextField();
		tfYan.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
				labelEmpty();
			}
		});
		GridBagConstraints gbc_tfYan = new GridBagConstraints();
		gbc_tfYan.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfYan.insets = new Insets(0, 0, 5, 5);
		gbc_tfYan.gridx = 2;
		gbc_tfYan.gridy = 4;
		getContentPane().add(tfYan, gbc_tfYan);
		tfYan.setColumns(10);
		
		labelYan = new JLabel("");
		GridBagConstraints gbc_labelYan = new GridBagConstraints();
		gbc_labelYan.insets = new Insets(0, 0, 5, 5);
		gbc_labelYan.gridx = 3;
		gbc_labelYan.gridy = 4;
		getContentPane().add(labelYan, gbc_labelYan);
		
		label = new JLabel("密码");
		GridBagConstraints gbc_label = new GridBagConstraints();
		gbc_label.insets = new Insets(0, 0, 5, 5);
		gbc_label.gridx = 1;
		gbc_label.gridy = 5;
		getContentPane().add(label, gbc_label);
		
		tfPassword = new JPasswordField();
		tfPassword.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
				labelEmpty();
				String yan = "";
				name=tfName.getText();
				email=tfEmail.getText();
				password=String.valueOf(tfPassword.getPassword());
				yan=tfYan.getText();
				if(yan.isEmpty()){
					return;
				}
				if(Integer.parseInt(yan)!=VAR.sixNum){
					labelYan.setText("验证码不正确");
					return;
				}
			}
		});
		GridBagConstraints gbc_tfPassword = new GridBagConstraints();
		gbc_tfPassword.insets = new Insets(0, 0, 5, 5);
		gbc_tfPassword.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfPassword.gridx = 2;
		gbc_tfPassword.gridy = 5;
		getContentPane().add(tfPassword, gbc_tfPassword);
		
		labelPassword = new JLabel("");
		GridBagConstraints gbc_labelPassword = new GridBagConstraints();
		gbc_labelPassword.insets = new Insets(0, 0, 5, 5);
		gbc_labelPassword.gridx = 3;
		gbc_labelPassword.gridy = 5;
		getContentPane().add(labelPassword, gbc_labelPassword);
		
		JButton btnNewButton_1 = new JButton("注册");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				labelEmpty();
				String yan = "";
				String tfPasswordConfire="";
				long account=0;
				String sql = "insert into user (name,email,password,account) values (?,?,?,?)";
				name=tfName.getText();
				email=tfEmail.getText();
				password=String.valueOf(tfPassword.getPassword()); 
				tfPasswordConfire = String.valueOf(tfPasswordCnfir.getPassword());
				account=getAccount();
				yan=tfYan.getText();
				if(yan.isEmpty()|account==0|tfPasswordConfire.isEmpty()){  //处理一些小暂时用简单的非空判断
					return;
				}
				if(Integer.parseInt(yan)!=VAR.sixNum){
					labelYan.setText("验证码不正确");
					return;
				}
				if(!password.matches("^[a-zA-Z0-9]{6,30}$")){
					labelPassword.setText("密码由6位以上字母和数字");
					return;
				}
				if(!password.equals(tfPasswordConfire)){
					labelPassword.setText("两次输入的密码不一样");
					return;
				}
				user.setEmail(tfEmail.getText().toString());//jdbc还没有写好，不能直接存user
				user.setName(tfName.getText().toString());
				int i=0;
				i = jdao.update(sql, new Object[]{name,email,password,account});
				if(i==1){
					labelPassword.setText("注册成功");
					RegFrame.this.setVisible(false);
				}
				System.out.println(i);
			}
		});
		
		label_1 = new JLabel("确认密码");
		GridBagConstraints gbc_label_1 = new GridBagConstraints();
		gbc_label_1.insets = new Insets(0, 0, 5, 5);
		gbc_label_1.gridx = 1;
		gbc_label_1.gridy = 6;
		getContentPane().add(label_1, gbc_label_1);
		
		tfPasswordCnfir = new JPasswordField();
		GridBagConstraints gbc_tfPasswordCnfir = new GridBagConstraints();
		gbc_tfPasswordCnfir.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfPasswordCnfir.insets = new Insets(0, 0, 5, 5);
		gbc_tfPasswordCnfir.gridx = 2;
		gbc_tfPasswordCnfir.gridy = 6;
		getContentPane().add(tfPasswordCnfir, gbc_tfPasswordCnfir);
		GridBagConstraints gbc_btnNewButton_1 = new GridBagConstraints();
		gbc_btnNewButton_1.insets = new Insets(0, 0, 5, 5);
		gbc_btnNewButton_1.gridx = 2;
		gbc_btnNewButton_1.gridy = 7;
		getContentPane().add(btnNewButton_1, gbc_btnNewButton_1);
		
	}
	public void labelEmpty(){
		labelYan.setText("");
		labelName.setText("");
		labelPassword.setText("");
	}
	public long getAccount(){
		Date date = new Date();
		return date.getTime()/1000;
	}
}
class MyTask extends TimerTask{
	public void run() {
		VAR.sixNum = VAR.getSixRandom();
	}
}